const Storage = {
  // _links 添加 登录之后和老登录字段的兼容，
  // 读取的实际是老的字段
  _links: {
    apiToken: 'account_api_token',
    identityToken: 'account_id_token',
    userInfo: 'account_user'
  },
  get: function(key) {
    let value = window.localStorage.getItem(key)
    if (this._links[key]) {
      value = window.localStorage.getItem(this._links[key])
    }
    try {
      const result = JSON.parse(value)
      return result
    } catch (e) {
      return {}
    }
  },

  set: function(key, value) {
    if (value === null) {
      this.remove(key)
      return
    }
    window.localStorage.setItem(key, JSON.stringify(value))
    if (this._links[key]) {
      this.set(this._links[key], value)
    }
  },

  remove: function(key) {
    window.localStorage.removeItem(key)
    if (this._links[key]) {
      this.remove(this._links[key])
    }
  },

  clear: function() {
    window.localStorage.clear()
  }
}

export default Storage
